

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <title>角色管理</title>
    <#include "/common/link.ftl">

</head>
<body class="hold-transition skin-black sidebar-mini">
<div class="wrapper">
    <#include "/common/navbar.ftl">
    <!--菜单回显-->
    <#assign currentMenu="role"/>
    <#include "/common/menu.ftl">
    <div class="content-wrapper">
        <section class="content-header">
            <h1>角色编辑</h1>
        </section>
        <section class="content">
            <div class="box">
                <form class="form-horizontal" action="/role/saveOrUpdate" method="post" id="editForm">

                    <input type="hidden"  name="id" value="${role.id}">
                    <div class="form-group"  style="margin-top: 10px;">
                        <label  class="col-sm-2 control-label">角色名称：</label>
                        <div class="col-sm-6">
                            <input type="text" class="form-control"  name="name"  placeholder="请输入角色名称" value="${role.name}">
                        </div>
                    </div>

                    <div class="form-group">
                        <label class="col-sm-2 control-label">角色编号：</label>
                        <div class="col-sm-6">
                            <input type="text" class="form-control"  name="sn"  placeholder="请输入角色编号" value="${role.sn}">
                        </div>
                    </div>
                    <div class="form-group " id="role">
                        <label for="role" class="col-sm-2 control-label">分配权限：</label><br/>
                        <div class="row" style="margin-top: 10px">
                            <div class="col-sm-2 col-sm-offset-2">
                                <select multiple class="form-control allPermissions" size="15">
                                    <#list permissions as p>
                                        <option value="${p.id}">${p.name}</option>
                                    </#list>
                                </select>
                            </div>

                            <div class="col-sm-1" style="margin-top: 60px;" align="center">
                                <div>

                                    <a type="button" class="btn btn-primary" style="margin-top: 10px" title="右移动"
                                       onclick="moveSelected('allPermissions', 'selfPermissions')">
                                        <span class="glyphicon glyphicon-menu-right"></span>
                                    </a>
                                </div>
                                <div>
                                    <a type="button" class="btn btn-primary " style="margin-top: 10px" title="左移动"
                                       onclick="moveSelected('selfPermissions', 'allPermissions')">
                                        <span class="glyphicon glyphicon-menu-left"></span>
                                    </a>
                                </div>
                                <div>
                                    <a type="button" class="btn btn-primary " style="margin-top: 10px" title="全右移动"
                                       onclick="moveAll('allPermissions', 'selfPermissions')">
                                        <span class="glyphicon glyphicon-forward"></span>
                                    </a>
                                </div>
                                <div>
                                    <a type="button" class="btn btn-primary " style="margin-top: 10px" title="全左移动"
                                       onclick="moveAll('selfPermissions', 'allPermissions')">
                                        <span class="glyphicon glyphicon-backward"></span>
                                    </a>
                                </div>
                            </div>
                            <div class="col-sm-2">
                                <select multiple class="form-control selfPermissions" size="15" name="ids">
                                    <#list role.permissions as p>
                                        <option value="${p.id}">${p.name}</option>
                                    </#list>
                                </select>
                            </div>


                        </div>
                    </div>
                    <div class="form-group">
                        <div class="col-sm-offset-1 col-sm-6">
                            <button type="button" class="btn btn-primary btn-submit">保存</button>
                            <a href="javascript:window.history.back()" class="btn btn-danger">取消</a>
                        </div>
                    </div>

                </form>
            </div>
        </section>
    </div>
    <#include "/common/footer.ftl">
</div>
<script>
    // allRoles:源    selfRoles:目标对象
    //根据用户传入的class值,拿到对应的想要把的dom对象,进行转移
    function moveSelected(allRoles,selfRoles) {
        var  src = $('.'+allRoles+'> option:selected');
        var  des = $('.'+selfRoles);
        des.append(src);
    }

    function moveAll(allRoles,selfRoles) {
        var  src = $('.'+allRoles+'> option');
        var  des = $('.'+selfRoles);
        des.append(src);
    }

    //当用户进行复杂的操作时,会导致数据的丢失,
    // 比如说,当用户将左边的角色全部赋值给右边,可是右边的角色并没有选中
    //这样导致没被选中的角色没有被提交到后台
    //所以在点击保存之前,我们应该将右边的选项全部设置为选中
    //而submit按钮,点击就直接提交了,不可以进行操作,button可以进行操作
    $('.btn-submit').click(function () {
        // 在点击按钮时,将option的属性设置为选中
        $('.selfPermissions > option').prop('selected',true);
        //提交表单
        $('#editForm').submit();
    })







    //option的去重
    //拿到右边的存到数组中,遍历左边的进行对比,看是否包含右边的,包含的话则删除左边的额

    //拿到右边的数据存入数组
    var arr = []; //定义一个数组
    $('.selfPermissions > option').each(function (index,element) {  //遍历拿到所有option的存入数组
        // console.log(element)  通过打印,我们知道element是个dom对象
        arr.push($(element).val());
    })

    $('.allPermissions > option').each(function (index,element) {
        // console.log(element)
        //查看左边的角色是否已经在右边的角色,
        if ($.inArray($(element).val(), arr) > -1) {  //如果右边包含左边的角色,则删除掉左边角色
            $(element).remove();
        }

    })






</script>
</body>
</html>
